package com.kuang.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuang.common.utils.PageUtils;
import com.kuang.common.utils.Query;
import com.kuang.gulimall.order.dao.OrderItemDao;
import com.kuang.gulimall.order.entity.OrderEntity;
import com.kuang.gulimall.order.entity.OrderItemEntity;
import com.kuang.gulimall.order.entity.OrderReturnReasonEntity;
import com.kuang.gulimall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"xiangmou_java_test1"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**接收消息
     *queues，声明要收听的所有队列
     * 参数可以写一下类型：
     * 1，Message message ：原生信息详细信息。头+体
     * 2，T<发送的消息类型> Message message, OrderReturnReasonEntity content,会自动接受到实体类
     * 3，Channel channel:当前传输数据的通道
     *
     * Queue:可以很多人监听。只要收到消息，队列删除消息，而且只能有同一个收到此消息
     * 场景：
     *      1，订单服务启动多个；同一个消息，只能有一个客户端收到
     *      2，只有一个消息完全处理完，方法运行结束，我们就可以接收到下一个消息
     *      RabiitHandler，
     */
//    @RabbitListener(queues = {"xiangmou_java_test1"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        byte[] body = message.getBody();
        MessageProperties messageProperties = message.getMessageProperties();

        System.out.println("接受到消息。。内容"+body+"==>属性："+messageProperties+"==>内容："+content);
        //前面是特殊的数字，通道内自增的，后面是是否批量签收
        //签收货物
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            //网络中断
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage(OrderEntity content) throws InterruptedException {


        System.out.println("jie"+content);
    }

}